//
//  regisViewController.m
//  UI_13豆瓣作业
//
//  Created by dllo on 16/1/3.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "regisViewController.h"
#import "LTView.h"
#import "loginViewController.h"
#import "DataBaseHandle.h"
#import "UserModel.h"
@interface regisViewController ()

@property (nonatomic, retain)LTView *view1; /** < 用户名*/
@property (nonatomic, retain)LTView *view2; /** < 密码*/
@property (nonatomic, retain)LTView *view3; /** < 确认密码*/
@property (nonatomic, retain)LTView *view4; /** < 邮箱*/
@property (nonatomic, retain)LTView *view5; /** < 联系方式*/

@end

@implementation regisViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    // tabBar的隐藏
    self.tabBarController.tabBar.hidden = YES;
    
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(item2Action:)];
    self.navigationItem.rightBarButtonItem = item2;

    
    self.view1 = [[LTView alloc] initWithFrame:CGRectMake(20, 20, 350, 40)];
    _view1.myLabel.text = @"用户名";
    _view1.myTextField.placeholder = @"请输入用户名";
    [self.view addSubview:_view1];
    [_view1 release];
    
    self.view2 = [[LTView alloc] initWithFrame:CGRectMake(20, 80, 350, 40)];
    _view2.myLabel.text = @"密码";
    _view2.myTextField.placeholder = @"请输入密码";
    _view2.myTextField.secureTextEntry = YES;
    [self.view addSubview:_view2];
    [_view2 release];
    
    self.view3 = [[LTView alloc] initWithFrame:CGRectMake(20, 140, 350, 40)];
    self.view3.myLabel.text = @"确认密码";
    self.view3.myTextField.placeholder = @"请再次输入密码";
    self.view3.myTextField.secureTextEntry = YES;
    [self.view addSubview:self.view3];
    [_view3 release];

    self.view4 = [[LTView alloc] initWithFrame:CGRectMake(20, 200, 350, 40)];
    self.view4.myLabel.text = @"邮箱";
    self.view4.myTextField.placeholder = @"请输入邮箱";
    [self.view addSubview:self.view4];
    [_view4 release];
    
    self.view5 = [[LTView alloc] initWithFrame:CGRectMake(20, 260, 350, 40)];
    self.view5.myLabel.text = @"联系方式";
    self.view5.myTextField.placeholder = @"请输入联系方式";
    [self.view addSubview:self.view5];
    [_view5 release];
}

// 点击注册方法
- (void) item2Action:(UIBarButtonItem *)item2{
    
    [[DataBaseHandle sharedDataBaseHandle] createTable];
    
    if (![_view1.myTextField.text isEqualToString:@""] && ![_view2.myTextField.text isEqualToString:@""] && ![_view3.myTextField.text isEqualToString:@""]) {
        // 先从数据库判断用户是否已经存在
        UserModel *user = [[DataBaseHandle sharedDataBaseHandle] selectUser:_view1.myTextField.text];
      
        if ([_view2.myTextField.text isEqualToString:_view3.myTextField.text]) {
              // 如果两次密码都相同并且用户不存在，则注册
            if (user.userName == nil) {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:alert animated:YES completion:nil];
                [self dismissViewControllerAnimated:YES completion:nil];
                
                UserModel *model = [UserModel userModel];
                model.userName = self.view1.myTextField.text;
                model.password = self.view2.myTextField.text;
                model.email = self.view4.myTextField.text;
                model.telepnone = self.view5.myTextField.text;
                model.state = @"否";
                // 插入用户表
                [[DataBaseHandle sharedDataBaseHandle] insertUserModel:model];

            } else {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"用户已存在" preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:alert animated:YES completion:nil];
                [self dismissViewControllerAnimated:YES completion:nil];

            }
            
            
        } else {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"请确认两次密码一致" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:nil];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    } else  if([_view1.myTextField.text isEqualToString:@""]){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名不能为空" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        [self dismissViewControllerAnimated:YES completion:nil];
    } else if (![_view1.myTextField.text isEqualToString:@""] && [_view2.myTextField.text isEqualToString:@""] ) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        [self dismissViewControllerAnimated:YES completion:nil];
    } else if (![_view1.myTextField.text isEqualToString:@""] && ![_view2.myTextField.text isEqualToString:@""] && [_view3.myTextField.text isEqualToString:@""]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请再次输入密码" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        [self dismissViewControllerAnimated:YES completion:nil];
    }

}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
